package com.hexinfo.dmpro.dev.data.transfer.model;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.hexinfo.dmpro.dev.data.transfer.common.enums.DevDataExportModelEnum;
import com.hexinfo.dmpro.dev.data.transfer.common.enums.DevDataTransferExportTaskStatusEnum;
import com.hexinfo.dmpro.dev.data.transfer.common.enums.DevDataTransferImportTaskStatusEnum;
import io.swagger.annotations.ApiModel;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Date;

import org.springframework.validation.annotation.Validated;

/**
 * 总导入任务表
 *
 * @author yyh.huang
 * @date 2023-10-25 19:18:17
 */
@Data
@Validated
@TableName("TD_DEV_DATA_IMPORT_TASK")
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "总导入任务表")
public class DevDataImportTask extends Model<DevDataImportTask> {
    private static final long serialVersionUID = 1L;

    /**
     * ID
     */
    @TableId(type = IdType.ASSIGN_UUID)
    @ApiModelProperty(value = "ID")
    private String id;
    /**
     * 任务名
     */
    @ApiModelProperty(value = "任务名")
    private String taskName;
    /**
     * 以秒级时间戳为任务ID
     */
    @ApiModelProperty(value = "以秒级时间戳为任务ID")
    private String taskCode;
    /**
     * 导出总数
     */
    @ApiModelProperty(value = "导出总数")
    private Integer importTotal;
    /**
     * 导出成功数量
     */
    @ApiModelProperty(value = "导出成功数量")
    private Integer importSuccess;
    /**
     * 导出失败数量
     */
    @ApiModelProperty(value = "导出失败数量")
    private Integer importFail;
    /**
     * 导入文件转储位置
     */
    @ApiModelProperty(value = "导入文件转储位置")
    private String filePath;
    /**
     * 任务状态
     */
    @ApiModelProperty(value = "任务状态")
    private String taskStatus;
    /**
     * 任务开始时间
     */
    @ApiModelProperty(value = "任务开始时间")
    private Date startTime;
    /**
     * 任务结束时间
     */
    @ApiModelProperty(value = "任务结束时间")
    private Date endTime;

    @ApiModelProperty(value = "错误信息")
    private String errorMessage;

    @ApiModelProperty(value = "数据同步库名")
    private String dbName;

    @ApiModelProperty(value = "数据同步模式")
    private String transferModel;

    @ApiModelProperty(value = "数据同步类型")
    private String transferType;

    @ApiModelProperty(value = "数据同步时间区间")
    private String timeInterval;

    /**
     * 创建人
     */
    @ApiModelProperty(value = "创建人")
    @TableField(fill = FieldFill.INSERT)
    private String createBy;
    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间")
    @TableField(fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" ,timezone = "GMT+8")
    private Date createTime;
    /**
     * 更新人
     */
    @ApiModelProperty(value = "更新人")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private String lastUpdateBy;
    /**
     * 更新时间
     */
    @ApiModelProperty(value = "更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" ,timezone = "GMT+8")
    private Date lastUpdateTime;
    /**
     * 删除标识
     */
    @ApiModelProperty(value = "删除标识")
    @TableField(fill = FieldFill.INSERT)
    private String del;

    @TableField(exist = false)
    private Integer buildCount;

    public String getTaskStatusName() {
        if (StrUtil.isNotBlank(taskStatus)) {
            return DevDataTransferImportTaskStatusEnum.valueOf(taskStatus).getName();
        }
        return "";
    }

    public String getStartTimeString() {
        if (startTime != null) {
            return DateUtil.formatDateTime(startTime);
        }
        return "";
    }

    public String getEndTimeString() {
        if (endTime != null) {
            return DateUtil.formatDateTime(endTime);
        }
        return "";
    }

    public String getTimeConsuming() {
        if (startTime != null && endTime != null) {
            long seconds = DateUtil.between(startTime, endTime, DateUnit.SECOND);
            if (seconds < 60) {
                return seconds + "秒";
            } else {
                return StrUtil.format("{}分{}秒", seconds / 60, seconds % 60);
            }
        }
        return "-";
    }

    public String getTransferModelString(){
        return (StrUtil.isBlank(transferModel) ? "" : StrUtil.format("【{}】", StrUtil.isBlank(transferModel) ? "" : DevDataExportModelEnum.valueOf(transferModel).getName().replace("导出", "")))
                + (StrUtil.isBlank(transferType) ? "" : (StrUtil.equals(transferType, "custom") ? "" : (StrUtil.equals(transferType, "complete") ? "【全量】" : (StrUtil.equals(transferType, "increment") ? "【增量】" : ""))))
                + (StrUtil.isBlank(dbName) ? "" : StrUtil.format("【{}】", dbName))
                + (StrUtil.isBlank(timeInterval) ? "" : (StrUtil.equals(transferType, "complete") ? "" : StrUtil.format("【{}】", timeInterval)));
    }
}
